<?php
	require_once('/admin_header.php');
	require_once('ticket_functions.php');
	require_once('email.php');
	require_once('globals.php');
	
	if($_SERVER['REQUEST_METHOD'] == 'POST'):
		handlePost();				
	elseif ($_SERVER['REQUEST_METHOD'] == 'GET'):
		// TODO: check if ticket exists
		if(isset($_GET['ticketID'])):
			$ticketNumber = $_GET['ticketID'];
			displayEmailForm($ticketNumber);
		else:
			header("Location: /proj2/admin_page.php");
		endif;
		
	endif;
	
	function handlePost()
	{
		if(isset($_POST['sendEmailSubmit'])):
			$sender = sanitizeStringKeepSpaces($_POST['emailSender']);
			$receiver = sanitizeStringKeepSpaces($_POST['emailReceiver']);
			$subject = sanitizeStringKeepSpaces($_POST['emailSubject']);
			$message = sanitizeStringKeepSpaces($_POST['emailMessage']);
			if(!empty($subject) && !empty($message)):
				sendEmail($sender, $receiver, $subject, $message);
				echo "<p>Sent email to $receiver</p>";
			else:
				$ticketNumber = $_POST['ticketID'];
				header("Location: email_user.php?ticketID=" . $ticketNumber);
			endif;
		else:
			header("Location: admin_page.php");
		endif;
	}
	
	function displayEmailForm($ticketNumber)
	{
		if(!ticketExists($ticketNumber)):
			echo "<p>Ticket #$ticketNumber does not exist.</p>";
			return;
		endif;
		echo '<form action="email_user.php" method="post">';
		echo '<h2>Send email to ticket submitter</h2>';
		$row = getTicketInfo($ticketNumber);
		$sender = getThisAdminsEmailAddress(); // the currently logged in admin's email
		$receiver = $row['sender_email']; // submitter email
		echo "Sender (admin): $sender<br/><br/>\nRecipient (submitter of ticket): $receiver <br/> \n";
		echo '<input type="hidden" name="emailSender" value="' . $sender . '"\n>';
		echo '<input type="hidden" name="emailReceiver" value="' . $receiver . '"\n>';
		echo '<input type="hidden" name="ticketID" value="' . $ticketNumber . '"\n>';
		$ticketSubject = $row['subject'];
		echo 'Subject: <input style="width: 400px;" type="text" name="emailSubject" value="Concerning Ticket #' . $ticketNumber . ' \'' . $ticketSubject .'\'"> </br>';
		echo 'Message:<br/> <textarea name="emailMessage" type="text" rows="6" cols="50"></textarea> <br/>';
		echo '<input type="submit" value="Send Email" name="sendEmailSubmit" >';
		echo '</form>';
	}
	
	function getAdminInfoByID($adminID)
	{
		require('\private\mysqli_connect.php');
		$query = "select * from administrators where admin_id = " . $adminID;
	}
	
	function ticketExists($ticketNumber)
	{
		require('\private\mysqli_connect.php');
		$query = 'select ticket_id from tickets where ticket_id = ' . $ticketNumber;
		$result = $db->query($query);
		// echo "$query<br/>";
		if($result->num_rows > 0):
			// echo "ticket exists";
			return true;
		else:
			// echo "ticket does not exist";
			return false;
		endif;
	}
	
	function getThisAdminsEmailAddress()
	{
		require('\private\mysqli_connect.php');
		$query = 'select email from administrators where admin_id = ' . $_SESSION['adminID'];
		$result = $db->query($query);
		// echo "$query<br/>";
		if($result->num_rows > 0):
			// echo "ticket exists";
			$row = $result->fetch_array();
			return $row['email'];
		else:
			// echo "ticket does not exist";
			return null;
		endif;
	}

?>